﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.Security;

namespace SPCIT.SP2010.Portal.ChangeMembershipPassword
{
    [ToolboxItemAttribute(false)]
    public class ChangeMembershipPassword : SPCITProvidersBaseWebPart
    {
        protected DropDownList loginNames;
        protected TextBox oldPassword;
        protected TextBox newPassword;
        protected TextBox confirmNewPassword;
        protected CompareValidator comparePasswords;

        protected Button changeCommand;
        protected Label result;

        protected override void CreateChildControls()
        {
            this.loginNames = new DropDownList();
            this.Controls.Add(this.loginNames);

            this.oldPassword = new TextBox();
            this.oldPassword.TextMode = TextBoxMode.Password;
            this.Controls.Add(this.oldPassword);

            this.newPassword = new TextBox();
            this.newPassword.ID = "newPassword";
            this.newPassword.TextMode = TextBoxMode.Password;
            this.Controls.Add(this.newPassword);

            this.confirmNewPassword = new TextBox();
            this.confirmNewPassword.ID = "confirmNewPassword";
            this.confirmNewPassword.TextMode = TextBoxMode.Password;
            this.Controls.Add(this.confirmNewPassword);

            this.comparePasswords = new CompareValidator();
            this.comparePasswords.ControlToValidate = this.newPassword.ID;
            this.comparePasswords.ControlToCompare = this.confirmNewPassword.ID;
            this.comparePasswords.Operator = ValidationCompareOperator.Equal;
            this.comparePasswords.Type = ValidationDataType.String;
            this.comparePasswords.Text = "*";
            this.comparePasswords.Display = ValidatorDisplay.Dynamic;
            this.Controls.Add(this.comparePasswords);

            this.changeCommand = new Button();
            this.changeCommand.Click += new EventHandler(changeCommand_Click);
            this.changeCommand.Text = "Cambia Password";
            this.Controls.Add(this.changeCommand);

            this.result = new Label();
            this.result.Text = String.Empty;
            this.Controls.Add(this.result);

            base.CreateChildControls();
        }

        protected override void OnLoad(EventArgs e)
        {
            this.EnsureChildControls();

            base.OnLoad(e);

            SPWeb webContext = SPControl.GetContextWeb(Context);

            if ((!this.Page.IsPostBack) 
                && (webContext.CurrentUser != null)
                && (webContext.CurrentUser.IsSiteAdmin))

            {
                bindLoginNames();
            }
        }

        private void bindLoginNames()
        {
            this.loginNames.Items.Clear();
            Int32 totalRecords;
            this.loginNames.DataSource = this.GetConfiguredMembershipProvider().GetAllUsers(0, Int32.MaxValue, out totalRecords);
            this.loginNames.DataValueField = "UserName";
            this.loginNames.DataTextField = "UserName";
            this.loginNames.DataBind();
        }

        void changeCommand_Click(object sender, EventArgs e)
        {
            if (this.newPassword.Text == this.confirmNewPassword.Text)
            {
                SPWeb webContext = SPControl.GetContextWeb(Context);
                String loginName = webContext.CurrentUser.Name;

                if ((webContext != null)
                    && (webContext.CurrentUser != null)
                    && (webContext.CurrentUser.IsSiteAdmin))
                {
                    loginName = loginNames.SelectedValue;
                }

                try
                {
                    if (this.GetConfiguredMembershipProvider() != null)
                    {
                        if (this.GetConfiguredMembershipProvider().ChangePassword(loginName, this.oldPassword.Text, this.newPassword.Text))
                        {
                            this.result.Text = "<BR>La password è stata cambiata.";
                        }
                        else
                        {
                            this.result.Text = "<BR>La password non può essere modificata.";
                        }
                    }
                    else
                    {
                        this.result.Text = "<BR>Membership provider non configurato.";
                    }
                }
                catch (Exception ex)
                {
                    this.result.Text = "<BR>Errore durante il cambiamento della password.<BR>" + ex.Message + "<BR>" + ex.StackTrace;
                }
            }
            else
            {
                result.Text = "<BR>La password e la conferma password non corrispondono!";
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            String loginName = string.Empty;
            SPWeb webContext = SPControl.GetContextWeb(Context);

            try
            {
                loginName = webContext.CurrentUser.Name;
            }
            catch (Exception)
            {
                this.result.Text = "<BR>Effettuare l'accesso prima di usare questa WebPart.";
            }

            if (loginName != String.Empty)
            {
                writer.Write("<table border=0>");

                if ((webContext != null)
                    && (webContext.CurrentUser != null)
                    && (webContext.CurrentUser.IsSiteAdmin))
                {
                    writer.Write("<tr>");
                    writer.Write("<td class='ms-vb'>");
                    writer.Write("Utente:");
                    writer.Write("</td>");
                    writer.Write("<td class='ms-vb'>");
                    this.loginNames.RenderControl(writer);
                    writer.Write("</td>");
                    writer.Write("<td class='ms-vb'>");
                    writer.Write("</td>");
                    writer.Write("</tr>");
                }

                writer.Write("<tr>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("Vecchia password:");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                this.oldPassword.RenderControl(writer);
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("</tr>");

                writer.Write("<tr valign='top'>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("Nuova password:");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                this.newPassword.RenderControl(writer);
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("</tr>");

                writer.Write("<tr valign='top'>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("Conferma nuova password:");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                this.confirmNewPassword.RenderControl(writer);
                this.comparePasswords.RenderControl(writer);
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</tr>");

                writer.Write("<tr valign='top'>");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                this.changeCommand.RenderControl(writer);
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("</tr>");

                writer.Write("</table>");
                this.result.RenderControl(writer);
            }
            else
            {
                this.result.Text = "<BR>WebPart non attiva in questo contesto.";
                this.result.RenderControl(writer);
            }
        }
    }
}
